import { defineStore } from 'pinia'
import axios from 'axios'
import { useAuthStore } from './auth'

export const useTypeStore = defineStore('type', {
  state: () => ({
    types: [],
    loading: false,
    error: null
  }),

  actions: {
    // 获取当前用户的博客类型列表
    async getUserBlogTypes() {
      try {
        this.loading = true
        const authStore = useAuthStore()
        const userId = authStore.user?.userId
        
        if (!userId) {
          throw new Error('用户未登录')
        }
        
        const response = await axios.get(`/api/blog/type/user/${userId}`)
        this.types = response.data || []
        return this.types
      } catch (error) {
        this.error = error.response?.data?.message || '获取博客类型失败'
        throw error
      } finally {
        this.loading = false
      }
    },
    
    // 获取所有博客类型
    async getAllBlogTypes() {
      try {
        this.loading = true
        const response = await axios.get('/api/blog/types')
        this.types = response.data || []
        return this.types
      } catch (error) {
        this.error = error.response?.data?.message || '获取博客类型失败'
        throw error
      } finally {
        this.loading = false
      }
    },

    // 创建新的博客类型
    async createBlogType(typeName) {
      try {
        this.loading = true
        const authStore = useAuthStore()
        const userId = authStore.user?.userId
        
        if (!userId) {
          throw new Error('用户未登录')
        }
        
        const blogType = {
          userId: userId,
          typeName: typeName
        }
        
        const response = await axios.post('/api/blog/type/create', blogType)
        
        if (response.data.success) {
          // 创建成功后刷新类型列表
          await this.getUserBlogTypes()
          return response.data.blogType
        } else {
          throw new Error(response.data.message || '创建博客类型失败')
        }
      } catch (error) {
        this.error = error.response?.data?.message || error.message || '创建博客类型失败'
        throw error
      } finally {
        this.loading = false
      }
    },

    // 更新博客类型
    async updateBlogType(typeId, typeName) {
      try {
        this.loading = true
        const authStore = useAuthStore()
        const userId = authStore.user?.userId
        
        if (!userId) {
          throw new Error('用户未登录')
        }
        
        const blogType = {
          typeId: typeId,
          userId: userId,
          typeName: typeName
        }
        
        const response = await axios.put('/api/blog/type/update', blogType)
        
        if (response.data.success) {
          // 更新成功后刷新类型列表
          await this.getUserBlogTypes()
          return response.data.blogType
        } else {
          throw new Error(response.data.message || '更新博客类型失败')
        }
      } catch (error) {
        this.error = error.response?.data?.message || error.message || '更新博客类型失败'
        throw error
      } finally {
        this.loading = false
      }
    },

    // 删除博客类型
    async deleteBlogType(typeId) {
      try {
        this.loading = true
        const response = await axios.delete(`/api/blog/type/${typeId}`)
        
        if (response.data.success) {
          // 删除成功后刷新类型列表
          await this.getUserBlogTypes()
          return true
        } else {
          throw new Error(response.data.message || '删除博客类型失败')
        }
      } catch (error) {
        this.error = error.response?.data?.message || error.message || '删除博客类型失败'
        throw error
      } finally {
        this.loading = false
      }
    }
  }
}) 